# A valid snippet should starts with:
#
#		snippet trigger_word [ "description" [ options ] ]
#
# and end with:
#
#		endsnippet
#
# Snippet options:
#
#		b - Beginning of line.
#		i - In-word expansion.
#		w - Word boundary.
#		r - Regular expression
#		e - Custom context snippet
#		A - Snippet will be triggered automatically, when condition matches.
#
# Basic example:
#
#		snippet emitter "emitter properties" b
#		private readonly ${1} = new Emitter<$2>()
#		public readonly ${1/^_(.*)/$1/}: Event<$2> = this.$1.event
#		endsnippet
#
# Online reference: https://github.com/SirVer/ultisnips/blob/master/doc/UltiSnips.txt

global !p
def math():
	return vim.eval('vimtex#syntax#in_mathzone()') == '1'

def comment(): 
	return vim.eval('vimtex#syntax#in_comment()') == '1'

def env(name):
	[x,y] = vim.eval("vimtex#env#is_inside('" + name + "')") 
	return x != '0' and y != '0'

endglobal

###########################
# Sections and Paragraphs #
###########################

# None

###################
# Text formatting #
###################

snippet * "italics" r
*${1:${VISUAL}}*$0
endsnippet

snippet bold "bold" r
**${1:${VISUAL}}**$0
endsnippet

snippet ** "bold" r
**${1:${VISUAL}}**$0
endsnippet

snippet *** "bold italics" r
***${1:${VISUAL}}***$0
endsnippet

# snippet /* "Comment" r
# <!-- ${1:${VISUAL}} -->$0
# endsnippet

# ==========================================================
# Math
# ==========================================================

###################
#    Envirmoent   #
###################

snippet im "inline Math" r
\$${1:${VISUAL}}\$ $0 
endsnippet

snippet dm "Display Math" b
\$\$
	${1:${VISUAL}}
\$\$

$0
endsnippet

context "math()"
snippet align "Math aligned" bA
\\begin{aligned}
${1:${VISUAL}}
\\end{aligned}
endsnippet

context "math()"
snippet cases "Math Cases" bA
\\begin{cases}
$1
\\end{cases}
endsnippet

# TODO:矩阵
context "math()"
snippet bm "bold words" rA
{\\bm ${1:${VISUAL}}}
endsnippet

context "math()"
snippet rm "Regular words" i
{\\rm ${1:${VISUAL}}}
endsnippet

snippet beg "begin{} / end{}" bA
\\begin{$1}
	$0
\\end{$1}
endsnippet


###################
#      常用符号    #
###################

#      常用字母    #
context "math()"
snippet alpha "alpha" iA
\alpha
endsnippet

context "math()"
snippet beta "bata" iA
\beta
endsnippet

context "math()"
snippet gamma "gamma" iA
\gamma
endsnippet

#      常用片段    #
context "math()"
snippet range "i = 1, \ldots, n" i
${1:i} = ${2:1}, \ldots, ${3:n}
endsnippet

context "math()"
snippet list "a_1, ..., a_n" i
${1:i}_${2:1}, \ldots, $1_${3:n}
endsnippet

context "math()"
snippet pian "piandaoshu" wA
\partial 
endsnippet

context "math()"
snippet dot "\dot{}" wA
\dot{$1} $0
endsnippet
#      未归类    #
context "math()"
snippet ... "cdots" b
\cdots
endsnippet


snippet => "implies" Ai
\implies
endsnippet

snippet =< "implied by" Ai
\impliedby
endsnippet

context "math()"
snippet iff "iff" Ai
\iff
endsnippet

snippet mk "Math" wA
$${1}$`!p
if t[2] and t[2][0] not in [',', '.', '?', '-', ' ']:
	snip.rv = ' '
else:
	snip.rv = ''
`$2
endsnippet




context "math()"
snippet // "Fraction" iA
\\frac{$1}{$2}$0
endsnippet

context "math()"
snippet / "Fraction" i
\\frac{${VISUAL}}{$1}$0
endsnippet

context "math()"
snippet '((\d+)|(\d*)(\\)?([A-Za-z]+)((\^|_)(\{\d+\}|\d))*)/' "symbol frac" wrA
\\frac{`!p snip.rv = match.group(1)`}{$1}$0
endsnippet

priority 1000
context "math()"
snippet '^.*\)/' "() frac" wrA
`!p
stripped = match.string[:-1]
depth = 0
i = len(stripped) - 1
while True:
	if stripped[i] == ')': depth += 1
	if stripped[i] == '(': depth -= 1
	if depth == 0: break;
	i-=1
snip.rv = stripped[0:i] + "\\frac{" + stripped[i+1:-1] + "}"
`{$1}$0
endsnippet

context "math()"
snippet '([A-Za-z])(\d)' "auto subscript" wrA
`!p snip.rv = match.group(1)`_`!p snip.rv = match.group(2)`
endsnippet

context "math()"
snippet '([A-Za-z])_(\d\d)' "auto subscript2" wrA
`!p snip.rv = match.group(1)`_{`!p snip.rv = match.group(2)`}
endsnippet


snippet sympy "sympyblock " w
sympy $1 sympy$0
endsnippet

priority 10000
snippet 'sympy(.*)sympy' "sympy" wr
`!p
from sympy import *
x, y, z, t = symbols('x y z t')
k, m, n = symbols('k m n', integer=True)
f, g, h = symbols('f g h', cls=Function)
init_printing()
snip.rv = eval('latex(' + match.group(1).replace('\\', '').replace('^', '**').replace('{', '(').replace('}', ')') + ')')
`
endsnippet

context "math()"
snippet != "not equals" iA
\neq 
endsnippet

context "math()"
snippet ceil "ceil" iA
\left\lceil $1 \right\rceil $0
endsnippet

context "math()"
snippet floor "floor" iA
\left\lfloor $1 \right\rfloor$0
endsnippet

context "math()"
snippet pmat "pmat" iA
\begin{pmatrix} $1 \end{pmatrix} $0
endsnippet



context "math()"
snippet () "left( right)" iA
\left( ${1:${VISUAL}} \right) $0
endsnippet



context "math()"
snippet lro "left( right)" i
\left( ${1:${VISUAL}} \right) $0
endsnippet

context "math()"
snippet lr| "left| right|" i
\left| ${1:${VISUAL}} \right| $0
endsnippet

context "math()"
snippet lrP "left\{ right\}" i
\left\\{ ${1:${VISUAL}} \right\\} $0
endsnippet

context "math()"
snippet lrb "left\{ right\}" i
\left\\{ ${1:${VISUAL}} \right\\} $0
endsnippet

context "math()"
snippet lrp "left[ right]" i
\left[ ${1:${VISUAL}} \right] $0
endsnippet

context "math()"
snippet lra "leftangle rightangle" iA
\left<${1:${VISUAL}} \right>$0
endsnippet

context "math()"
snippet conj "conjugate" iA
\overline{$1}$0
endsnippet

context "math()"
snippet sum "sum" w
\sum_{n=${1:1}}^{${2:\infty}} ${3:a_n z^n}
endsnippet

snippet taylor "taylor" w
\sum_{${1:k}=${2:0}}^{${3:\infty}} ${4:c_$1} (x-a)^$1 $0
endsnippet

snippet lim "limit" w
\lim_{${1:n} \to ${2:\infty}} 
endsnippet

snippet limsup "limsup" w
\limsup_{${1:n} \to ${2:\infty}} 
endsnippet

snippet prod "product" w
\prod_{${1:n=${2:1}}}^{${3:\infty}} ${4:${VISUAL}} $0
endsnippet

snippet part "d/dx" w
\frac{\partial ${1:V}}{\partial ${2:x}} $0
endsnippet

context "math()"
snippet sq "\sqrt{}" iA
\sqrt{${1:${VISUAL}}} $0
endsnippet

context "math()"
snippet sr "^2" iA
^2
endsnippet

context "math()"
snippet cb "^3" iA
^3
endsnippet

context "math()"
snippet td "to the ... power" iA
^{$1}$0
endsnippet

context "math()"
snippet rd "to the ... power" iA
^{($1)}$0
endsnippet

context "math()"
snippet __ "subscript" iA
_{$1}$0
endsnippet

snippet ooo "\infty" iA
\infty
endsnippet

context "math()"
snippet rij "mrij" i
(${1:x}_${2:n})_{${3:$2}\\in${4:\\N}}$0
endsnippet

snippet <= "leq" iA
\le 
endsnippet

snippet >= "geq" iA
\ge 
endsnippet

context "math()"
snippet EE "geq" iA
\exists 
endsnippet

context "math()"
snippet AA "forall" iA
\forall 
endsnippet

context "math()"
snippet xnn "xn" iA
x_{n}
endsnippet

context "math()"
snippet ynn "yn" iA
y_{n}
endsnippet


context "math()"
snippet xii "xi" iA
x_{i}
endsnippet

context "math()"
snippet yii "yi" iA
y_{i}
endsnippet

context "math()"
snippet xjj "xj" iA
x_{j}
endsnippet

context "math()"
snippet yjj "yj" iA
y_{j}
endsnippet

context "math()"
snippet xp1 "x" iA
x_{n+1}
endsnippet

context "math()"
snippet xmm "x" iA
x_{m}
endsnippet

snippet R0+ "R0+" iA
\\R_0^+
endsnippet

context "math()"
snippet mcal "mathcal" iA
\mathcal{$1}$0
endsnippet

snippet lll "l" iA
\ell
endsnippet

context "math()"
snippet nabl "nabla" iA
\nabla 
endsnippet

context "math()"
snippet xx "cross" iA
\times 
endsnippet

priority 100
context "math()"
snippet ** "cdot" iA
\cdot 
endsnippet

context "math()"
snippet norm "norm" iA
\|$1\|$0
endsnippet

priority 100
context "math()"
snippet '(?<!\\)(sin|cos|arccot|cot|csc|ln|log|exp|star|perp)' "ln" rwA
\\`!p snip.rv = match.group(1)`
endsnippet

priority 300
context "math()"
snippet dint "integral" wA
\int_{${1:-\infty}}^{${2:\infty}} ${3:${VISUAL}} $0
endsnippet

priority 200
context "math()"
snippet '(?<!\\)(arcsin|arccos|arctan|arccot|arccsc|arcsec|pi|zeta|int)' "ln" rw
\\`!p snip.rv = match.group(1)`
endsnippet


priority 100
context "math()"
snippet -> "to" iA
\to 
endsnippet

priority 200
context "math()"
snippet <-> "leftrightarrow" iA
\leftrightarrow
endsnippet

context "math()"
snippet !> "mapsto" iA
\mapsto 
endsnippet

context "math()"
snippet invs "inverse" iA
^{-1}
endsnippet

context "math()"
snippet compl "complement" iA
^{c}
endsnippet

context "math()"
snippet \\\ "setminus" i
\setminus
endsnippet
\
snippet >> ">>" iA
\gg
endsnippet

snippet << "<<" iA
\ll
endsnippet


snippet ~~ "~" iA
\sim 
endsnippet

context "math()"
snippet set "set" wA
\\{$1\\} $0
endsnippet

snippet || "mid" iA
 \mid 
endsnippet


context "math()"
snippet cc "subset" Ai
\subset 
endsnippet

context "math()"
snippet notin "not in " iA
\not\in 
endsnippet

context "math()"
snippet inn "in " iA
\in 
endsnippet

context "math()"
snippet NN "n" iA
\N
endsnippet

context "math()"
snippet Nn "cap" iA
\cap 
endsnippet

context "math()"
snippet UU "cup" iA
\cup 
endsnippet

context "math()"
snippet uuu "bigcup" iA
\bigcup_{${1:i \in ${2: I}}} $0
endsnippet

context "math()"
snippet nnn "bigcap" iA
\bigcap_{${1:i \in ${2: I}}} $0
endsnippet

context "math()"
snippet OO "emptyset" iA
\O
endsnippet

context "math()"
snippet RR "real" iA
\R
endsnippet

context "math()"
snippet QQ "Q" iA
\Q
endsnippet

context "math()"
snippet ZZ "Z" iA
\Z
endsnippet

snippet <! "normal" iA
\triangleleft 
endsnippet

snippet <> "hokje" iA
\diamond 
endsnippet


context "math()"
snippet '(?<!i)sts' "text subscript" irA
_\text{$1} $0
endsnippet

context "math()"
snippet tt "text" iA
\text{$1}$0{
endsnippet


snippet bigfun "Big function" iA
\begin{align*}
	$1: $2 &\longrightarrow $3 \\\\
	$4 &\longmapsto $1($4) = $0
.\end{align*}
endsnippet

snippet cvec "column vector" iA
\begin{pmatrix} ${1:x}_${2:1}\\\\ \vdots\\\\ $1_${2:n} \end{pmatrix}
endsnippet

priority 10
context "math()"
snippet "bar" "bar" riA
\overline{$1}$0
endsnippet

priority 100
context "math()"
snippet "([a-zA-Z])bar" "bar" riA
\overline{`!p snip.rv=match.group(1)`}
endsnippet

priority 10
context "math()"
snippet "hat" "hat" riA
\hat{$1}$0
endsnippet

priority 100
context "math()"
snippet "([a-zA-Z])hat" "hat" riA
\hat{`!p snip.rv=match.group(1)`}
endsnippet

snippet HH "H" iA
\mathbb{H}
endsnippet

snippet DD "D" iA
\mathbb{D}
endsnippet

snippet dd "rm d" iA
{\rm d}
endsnippet

snippet diff "diff" iA
\frac{{\rm d} $1}{{\rm d} $2} $0
endsnippet

# Slidev
snippet page "Add a new page" b
---
id: $1
---

$0
endsnippet

snippet click "Add a click" b
<v-click>

${1:${VISUAL}}

</v-click>

$0
endsnippet

snippet float "Add a float object" b
<div class="absolute top-0 right-10">

${1:${VISUAL}}

</div>
$0
endsnippet

# vim:ft=snippets
